<?php
if(!defined('DCS_ROOT')) {exit('Access Denied');} 

class ads {

	function __construct (){
		$this->url = getUrl();
		$action = $this->url['params']['1'];		
		require_once(APP_PATH."model/admin/init.php" );		
		if($action == 'add'){
			$this->add();
		}elseif($action == 'edit'){
			$this->edit();
		}elseif($action == 'del'){
			$this->del();
		}elseif($action == 'imagedel'){
			$this->imageDel();
		}else{
			$this->defaultAction();
		}
		exit();	
	}

	function defaultAction(){
		$tpl = new Template( "admin/ads/list.htm" );
		require_once(APP_PATH."model/admin/ads.php" );
		$DB  = getDB();
		$data = new Ads_Model ($DB);
		if($this->url['params']['1'] == 'page'){
				$pagetemp = $this->url['params']['2'];		
		}		
		$condition['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$condition['rows'] = '20';
		$condition['order'] = 'addtime';
		if(isPost()){
			$keyword = htmToText(addslashes(trim($_POST['keyword'])));
			if(!empty($keyword)){
				$condition['condition'] = " name LIKE '%$keyword%' ";
			}else{
				message('请输入要搜索的关键字！');
			}
			if (strlen($keyword) < 2) {
				message('关键字不能少于2个字符！');
			}
		}
		$newsList = $data->getList($condition);
		foreach($newsList as $k => $v ){
			$newsList[$k]['addtime'] = date("Y-m-d  H:i:s",$v['addtime']);
			if($v['type'] == '1'){
				$newsList[$k]['type'] = '图片';
			}elseif($v['type'] =='2'){
				$newsList[$k]['type'] = '代码';
			}else{
				$newsList[$k]['type'] = 'Flash';
			}
			$newsList[$k]['is_verify'] = $v['is_verify'] == '1' ? '<font color="green">有效</font>' : '<font color="red">无效</font>';
		}
		$tpl->assign('newsList', $newsList);
		$condition['pageUrl'] = '/admin/ads/page/';
		$condition['pageTotal']	= $data->total($condition);
		$tpl->assign("pageParam", makePage($condition));
		$tpl->output();
	}

	function add(){
		require_once(APP_PATH."model/admin/ads.php" );
		$DB  = getDB();
		$data = new Ads_Model( $DB );
		if(!isPost()){
			$tpl = new Template( "admin/ads/add.htm" );
			$tpl->output();
		}else{			
			foreach($_POST as $k => $v ){				
				$var[$k] = daddslashes(trim($v));
			}			
			$type = $var['type'];			
			if($type == '1'){
				$content = $_POST['source']['image'];
				$isupload = 'sourceimg';
			}elseif($type =='2'){
				$content = $_POST['source']['code'];
			}elseif($type =='3'){
				$content = $_POST['source']['flash'];
				$isupload = 'sourceflash';
			}else{
				message('请选择广告类型');
			}
			$var['content'] = $content;
			unset($var['source']);			
			if($isupload){	
				if(!empty($_FILES['sourceflash']['name']) || !empty($_FILES['sourceimg']['name'])){
					require_once(CLASS_PATH."Class/Upload.class.php" );
					$upload = new Upload();
					$upload->config(array(
						'targetDir' => 'ads/',
						'saveType' => 2,
						'thumbStatus' => 0,					
						'waterMarkStatus' => '0',
						'imageConfig' => array('waterMarkStatus'=>'0')
					));
					$files = $upload->saveFiles($isupload);
					$var['files'] = $files['0']['filenames'];
				}
			}			
			$var['is_verify'] = trim($_POST['is_verify']);
			$var['url'] = $content['url'];
			$var['width'] = $content['width'];
			$var['height'] = $content['height'];
			unset($var['content']);

			$add = $data->addOne($var);			
			if ($add){
				
				$message = '成功增加广告';
			}else{
				$message = '增加广告失败';
			}
			updateAds();
			message($message);	
		}
	}

	function edit(){
		require_once(APP_PATH."model/admin/ads.php" );
		$DB  = getDB();
		$data = new Ads_Model( $DB );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条信息。');
		}
		$ads = $data->getOne($id);
		de($ads);
		if(!isPost()){
			$tpl = new Template("admin/ads/edit.htm" );	
			$ads['is_verify'] = $ads['is_verify'] == '1' ? 'checked' : '';
			if($ads['type'] == '1'){
				$ads['adContent'] = '<img src="/static/upload/ads/'.$ads['files'].'" width="'.$ads['width'].'" height="'.$ads['height'].'"  /><br><br>';	
			}elseif($ads['type'] == '3'){
				$ads['adContent'] = '<object type="application/x-shockwave-flash" data="/static/upload/ads/'.$ads['files'].'"  width="'.$ads['width'].'" height="'.$ads['height'].'" > 
				<param name="movie" value="/static/upload/ads/'.$ads['files'].'"/>
				<embed src="/static/upload/ads/'.$ads['files'].'"  width="'.$ads['width'].'" height="'.$ads['height'].'" ></embed> 
				</object><br><br>';
			}elseif($ads['type'] == '2'){
				$ads['adContent'] = $ads['content'];
			}
			//de($ads);
			$tpl->assign($ads);
			$tpl->output();
		}else{
			foreach($_POST as $k => $v ){				
				$var[$k] = daddslashes(trim($v));
			}			
			$type = $var['type'];			
			if($type == '1'){
				$content = $_POST['source']['image'];
				$isupload = 'sourceimg';
			}elseif($type =='2'){
				$content = $_POST['source']['code'];
			}elseif($type =='3'){
				$content = $_POST['source']['flash'];
				$isupload = 'sourceflash';
			}else{
				message('请选择广告类型');
			}
			$var['content'] = $content;
			unset($var['source']);			
			if($isupload){				
				if(!empty($_FILES['sourceflash']['name']) || !empty($_FILES['sourceimg']['name'])){
					require_once(CLASS_PATH."Class/Upload.class.php" );
					$upload = new Upload();
					$upload->config(array(
						'targetDir' => 'ads/',
						'saveType' => 2,
						'thumbStatus' => 0,					
						'waterMarkStatus' => '0',
						'imageConfig' => array('waterMarkStatus'=>'0')
					));
					$files = $upload->saveFiles($isupload);
					$var['files'] = $files['0']['filenames'];
				}
			}			
			$var['is_verify'] = trim($_POST['is_verify']);
			$var['url'] = $content['url'];
			$var['width'] = $content['width'];
			$var['height'] = $content['height'];
			//unset($var['content']);		
			$edit = $data->editOne($id,$var);
			if ($edit || $addPic){
				$message = '修改广告成功。';
			}else{
				$message = '修改广告失败！';
			}
			updateAds();
			message($message);
		}
	}

	function del(){
		require_once(APP_PATH."model/admin/ads.php" );
		$DB  = getDB();
		$data = new Ads_Model( $DB );
		$id = $this->url['params']['2'];
		if(!is_numeric($id)){
			message('找不到此条广告。');
		}			
		$del = $data->delOne($id);
		if ($del){
			$message = '删除成功。';
		}else{
			$message = '删除失败！';
		}
		updateAds();
		message($message);			
		
	}
}
?>
